﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess

Public Class Group

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property
#End Region

    Public Shared Function RetrieveAll() As IQueryable(Of Group)
        Dim db As EquipmentDataContext = Database.Instance
        Return db.Groups.OrderBy(Function(ug) ug.Description)
    End Function

    Public Shared Function RetrieveAll(ByVal siteID As Integer, ByVal departmentID As Integer) As IQueryable(Of Group)
        Dim db As EquipmentDataContext = Database.Instance

        If departmentID > 0 Then
            Return From g In Database.Instance.Groups Where g.SiteID = departmentID Order By g.Description
        ElseIf siteID > 0 Then
            Return From g In Database.Instance.Groups Where g.SiteDepartment.ParentID = siteID Order By g.Description
        Else
            Return RetrieveAll()
        End If
    End Function

    Public Shared Function RetrieveAllByDepartmentID(ByVal departmentID As Integer) As IQueryable(Of Group)
        Dim db As EquipmentDataContext = Database.Instance
        Return From g In Database.Instance.Groups Where g.SiteID = departmentID Order By g.Description
    End Function

    Public Shared Function RetrieveAllByDepartmentIDFor(ByVal departmentID As Integer, ByVal groupIDs As List(Of Integer)) As IQueryable(Of Group)
        Dim db As EquipmentDataContext = Database.Instance
        Return From g In Database.Instance.Groups Where g.SiteID = departmentID AndAlso groupIDs.Contains(g.ID) Order By g.Description
    End Function

    Public Shared Function RetrieveAllBySiteIDFor(ByVal siteID As Integer, ByVal groupIDs As List(Of Integer)) As IQueryable(Of Group)
        Dim db As EquipmentDataContext = Database.Instance
        Return From g In Database.Instance.Groups Where g.SiteDepartment.ParentID = siteID AndAlso groupIDs.Contains(g.ID) Order By g.Description
    End Function

    Public Shared Function RetrieveAllBySiteID(ByVal siteID As Integer) As IQueryable(Of Group)
        Dim db As EquipmentDataContext = Database.Instance
        Return From g In Database.Instance.Groups Where g.SiteDepartment.ParentID = siteID Order By g.Description
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As Group
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From ug In db.Groups Where ug.ID = id
        Dim groupEntity As Group = result.FirstOrDefault()
        groupEntity.DBContext = db
        Return groupEntity
    End Function

    Public Shared Function Insert(ByVal siteID As Integer, ByVal description As String) As Group

        Dim _dataBase As EquipmentDataContext = Database.Instance

        Dim newGroup As New Group
        newGroup.Description = description
        newGroup.SiteID = siteID

        _dataBase.Groups.InsertOnSubmit(newGroup)
        Try
            _dataBase.SubmitChanges()

        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try

        Return newGroup
    End Function

    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext

            Try
                _dataBase.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        End If

    End Sub
End Class
